
require 'net/http'
require 'net/https'
require 'uri'

require 'rexml/document'

module Provider

  class VoipcheapCOM< AbstractProvider

    @sms_sender

    @login_name
    @password
    @own_number

    def initialize(sms_sender)
      @sms_sender = sms_sender
      @login_name = @sms_sender.get_main.login_name
      @password = @sms_sender.get_main.password
      @own_number = @sms_sender.get_main.own_number
    end

    def send(to, text)

      path = "/clx/sendsms.php?username=#{@login_name}&password=#{@password}&from=#{@own_number}&to=#{to}&text=#{text}"
      #send_uri = URI.parse(send_url)

      http = Net::HTTP.new("myaccount.voipcheap.com", 443)
      http.use_ssl = 1
      resp, data = http.get(path, nil)
      #data = Net::HTTP.get(URI.parse(send_url))
      doc = REXML::Document.new(data)
      root = doc.root
      
      version = root.elements[1].text
      result = root.elements[2].text
      resultstring = root.elements[3].text
      description = root.elements[4].text
      endcause = root.elements[5].text

      if version.eql? "1"
        if result.eql? "0"
          resultstring = root.elements[3].text
          description = root.elements[4].text
          endcause = root.elements[5].text
          sms_not_sent(description)
        elsif result.eql? "1"
          sms_sent
        end
      else
        sms_not_sent("SMS might be sent. (wrong xml version)")
      end


      #SmsWidget::Main.logger.debug "voipcheap send, data: #{doc}"
      #SmsWidget::Main.logger.debug "voipcheap send, version: #{version}"
      #SmsWidget::Main.logger.debug "voipcheap send, result: #{result}"
      #SmsWidget::Main.logger.debug "voipcheap send, data: #{data}"
      
     end

  end

end
